let redis // = require('ioredis')()
if (process.env.NODE_ENV === 'production') {
  redis = require('ioredis')({
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    family: 4,           // 4 (IPv4) or 6 (IPv6)
    db: 0
  })
} else {
  redis = require('ioredis')({
    port: 6380,          // Redis port
    host: '127.0.0.1',   // Redis host
    family: 4,           // 4 (IPv4) or 6 (IPv6)
    db: 1
  })
}

redis.on('error', (error) => {
  console.log('Redis connection error', error)
})

// redis
module.exports = {
  getRedis: async (key) => {
    let res
    await redis.get(key).then(ret => {
      console.log(`get ${key} result:`, ret)
      res = ret
    })
    return res
  },
   // 设置 redis
  setRedis: (key, value, time) => {
    if (time) { redis.set(key, value, 'EX', time) } else { redis.set(key, value) }
    console.log(`set key ${key} success`)
  },
  deleleRedis: (key) => {
    redis.set(key, null, 'EX', 1)
  }
}
